function initConsejos(){
    $("#img_consejos").click(showConsejos);
    $("#consejos_container").dialog({
         autoOpen: false,
         title: "Consejos",
         width: 900,
         minWidth: 900,
         height: 560
     });
}

function showConsejos(){
    $("#consejos_container").load(webRoot+"consejos/getList/"+(Math.random() * 1000), {}, onConsejosLoad);
}

function onConsejosLoad(data){
    $("#consejos_container").find("input[type='button']").button();
    $("#guardar_nuevo_consejo").click(agregarConsejo);
    $("#consejos_container").find("input[value='Modificar']").each(
        function(){
            $(this).click(function(){
                modificar($(this).attr("cons_id"));
            });
        }
    );

    $("#consejos_container").find("input[value='Borrar']").each(
        function(){
            $(this).click(function(){
                eliminar($(this).attr("cons_id"));
            })
        }
    );

    $("#consejos_container").find("input[value='Pdf']").each(
        function(){
            $(this).click(function(){
                pdf($(this).attr("cons_id"));
            })
        }
    );

    $("#consejos").accordion({
        autoHeight: false,
        navigation: true,
        change: function(){
            var active = $("#consejos").find(".ui-accordion-content").get($("#consejos").accordion("option","active"));
            var area = $(active).find("textarea");
            if($(area).attr("id_consejo") != undefined){
                var ed = tinyMCE.get($(area).attr("name"));
                ed.setProgressState(1);
                $.get(webRoot+"consejos/getConsejo/"+$(area).attr("id_consejo")+"/"+(Math.random() * 100), {}, function(data){
                    ed.setProgressState(0);
                    ed.setContent(data);
                });
            }
        }
    });

    tinyMCE.el = new Array();

    tinyMCE.init({
      theme : "advanced",
      mode : "textareas",
      plugins : "fullpage",
      theme_advanced_buttons3_add : "fullpage",

      plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",

    // Theme options
    theme_advanced_buttons1 : "newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,fontsizeselect,|,cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist",
    theme_advanced_buttons2 : "outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
    theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
    theme_advanced_toolbar_location : "top",
    theme_advanced_toolbar_align : "left",
    theme_advanced_statusbar_location : "bottom",
    theme_advanced_resizing : true,


      convert_urls : false
   });



    $(".pdf_consejos").dialog({
         autoOpen: false,
         title: "Impresión de Consejo",
         width: 960,
         minWidth: 700,
         height: 560,
         minHeight: 560
     });

    $("#consejos_container").dialog('open');
}

function agregarConsejo(){

    var parameters = {
      op: "add"
    };

    var ed = tinyMCE.get("consejo_nuevo");

    parameters["titulo"] = $("#titulo_nuevo").val();
    parameters["consejo"] = ed.getContent();

    save(parameters, ed);
}

function modificar(id){
    var parameters = {
      op: "edit"
    };

    var ed = tinyMCE.get("consejo_"+id);

    parameters["id"]    = id;
    parameters["titulo"] = $("#titulo_"+id).val();
    parameters["consejo"] = ed.getContent();

    save(parameters, ed);

}

function eliminar(id){
   var parameters = {
      op: "del"
    };

    parameters["id"]    = id;

    var ed = tinyMCE.get("consejo_"+id);

    save(parameters, ed);
}

function pdf(id){
    $("#divpdf_"+id).html("<iframe src='"+webRoot+"consejos/download/"+id+"' width='100%' height='500'>Cargando ...</iframe>");
    $("#divpdf_"+id).dialog('open');
}

function save(parameters, ed){
    ed.setProgressState(1);

    $.post(webRoot+"consejos/save", parameters, function(){
        ed.setProgressState(0);
        $("#consejos").find("textarea").each(function(){
            tinyMCE.execCommand('mceRemoveControl',false, $(this).attr("name"));
            $(this).css({visibility: "hidden"});
        });
        showConsejos();
    }, "json");
}